const path  = require('path')
const fs = require('fs')

export const dabounce = (fn, timeout) => {
  let timer = null
  return function(){
    if(timer){
      clearTimeout(timer)
      return
    }
    const args = arguments
    timer = setTimeout(() => {      
      fn.apply(this, args)
      timer = null
    }, timeout)
  }
}

export const vueFileSaveToProject = function(filename, str) {
  const tplVuePath = path.resolve(__dirname, '/exportsFile/')
  console.log('path:', tplVuePath)
  // const exit = fs.existsSync(tplVuePath) // 判断是否存在此文件目录
  // console.log('exit:', exit);
  // if(!exit) {
  //   fs.mkdirSync(path.join(__dirname, '..', './exportsFile/'))
  //   console.log('文件夹创建成功!');
  // }
  // 创建文件
  fs.writeFile(`${tplVuePath}${filename}`, str, {encoding: 'utf-8', flag: 'a'}, function(err) {
    if(err) {
      throw err
    }
      console.log('文件已生成:^_^')
  })
}

// fs.appendFile('name.txt', 'xmk', function(err) {
//   if(err){
//     throw err
//   }
//   console.log('文件生成成功:')
// })
